﻿using Login.DTO.User;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Login.DataAccess;
using Login.Database.Entity;
using Login.DataAccess.Implement;
using Utility.Exception;

namespace Login.Context.ContextMgm
{
    public interface IUserManagementContext
    {
        UserDTO VerifyUser(string userName, string passwordSha1);


    }

    class UserManagementContext : ContextBase, IUserManagementContext
    {
        public UserManagementContext(Guid handlerId)
        {
            HandlerId = handlerId;
        }
        public UserDTO VerifyUser(string userName, string passwordSha1)
        {
            var userRepository = DataaccessManagerFactory.CreateUsersRepository();
            var userEntiry = userRepository.Find(model => model.UserName == userName);
            if (userEntiry != null && userEntiry.Password.Equals(passwordSha1))
            {
                //return GetUser(userEntiry);
                return new UserDTO { Id = Guid.Empty, UserName = userEntiry.UserName, Name = "123", Email = userEntiry.UserName + "@123.com" };
            }
            throw new PasswordNotMatchException();
        }
        //private UserDTO GetUser(User user)
        //{
        //    UserDTO cache;
        //    if (TryCheckCacheUser(out cache, user.ID.ToString()))
        //    {
        //        return cache;
        //    }
        //    return RenewUser(user);
        //}

        //private bool TryCheckCacheUser(out UserDTO user, string userId)
        //{
        //    try
        //    {
        //        user = CacheHelper.GetInstance().Get<UserDTO>(userId);
        //        return user != null;
        //    }
        //    catch (Exception e)
        //    {
        //        Log.Error(e);
        //        user = null;
        //        return false;
        //    }
        //}

        //private UserDTO RenewUser(User user)
        //{
        //    var lbg = new LogByGroup(Log, "RenewUser").Start();

        //    var powers = new List<PowerDTO>();
        //    var menuDics = new Dictionary<string, MenuDTO>();

        //    var menuEntities = DataAccessManagerFactory.CreateMenusRepository().All(e => e.ViewPower).ToList();
        //    foreach (var userRoles in user.Roles.ToList())
        //    {
        //        foreach (var userRolesPower in userRoles.Powers.ToList())
        //        {
        //            var power = PowerTransfer.Transfer(userRolesPower);
        //            powers.Add(power);
        //            var viewMenuEntities = menuEntities.Where(d => d.ViewPower != null && d.ViewPower.Id == power.Id);
        //            foreach (var menuEntity in viewMenuEntities)
        //            {
        //                if (!menuDics.ContainsKey(menuEntity.Name) && menuEntity.Enabled)
        //                {
        //                    var menu = MenuTransfer.Transfer(menuEntity);
        //                    menuDics.Add(menuEntity.Name, menu);
        //                }
        //            }
        //        }
        //    }

        //    var topDept = DataAccessManagerFactory.CreateDeptsRepository().GetTopDept(user.Depts);

        //    UserDTO retVal;
        //    if (topDept != null)
        //    {
        //        retVal = UserTransfer.Transfer(user, menuDics.Values.OrderBy(m => m.SortIndex).ToList(), topDept.Code, topDept.Name);
        //    }
        //    else
        //    {
        //        retVal = UserTransfer.Transfer(user, menuDics.Values.OrderBy(m => m.SortIndex).ToList(), string.Empty, string.Empty);
        //    }

        //    SetCacheUser(retVal);
        //    SetCachePower(retVal.Id, powers.ToList());

        //    lbg.End();

        //    return retVal;
        //}

    }
}
